package test.demo.Error;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.sun.xml.internal.ws.developer.Serialization;
import lombok.Data;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import test.demo.Exception.BaseException;
import test.demo.Exception.SQLException;

import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
@Serialization
@Data
public class ErrorVO {

        //异常编码
        private Integer code;
        //http状态码
        private Integer httpStatus;
        //信息
        private String message;
        //发生异常的请求
        private String path;
        //异常发生时间戳
        private Instant timestamp;
        //异常附加信息
        private Map<String, Object> detail = new HashMap<>();

        public ErrorVO(BaseException e, String path) {
            this.code = e.getCode().getCode();
            this.httpStatus = e.getCode().getHttpStatus();
            this.timestamp = Instant.now();
            this.path = path;
            this.message = e.getCode().getMessage();
            if (ObjectUtils.isEmpty(e.getDetail())){
                this.detail = null;
            }else {
                this.detail.putAll(e.getDetail());
            }
        }


    public ErrorVO(SQLException e, String path) {
        this.code = e.getSQLCode();
        this.httpStatus=e.getSQLStatus();
        this.timestamp = Instant.now();
        this.path = path;
        this.message = e.getMessage();
    }


    public ErrorVO(HttpRequestMethodNotSupportedException e, String path) {
        this.code = e.getSupportedHttpMethods().size();
        this.httpStatus=e.getSupportedHttpMethods().hashCode();
        this.timestamp = Instant.now();
        this.path = path;
        this.message = e.getMessage();
    }


}

